<template>
	<button @click="pay" style="font-size:40px">pay</button>
</template>
<script>
import pingpp from 'pingpp-js'
import {wxPayLogin} from '@/assets/js/global.js'
import {getParameterByName} from '@/assets/js/utils.js'
export default{
	name: 'Pay',
	mounted(){
	},
	methods:　{
			pingpay(orderInfo, successCb, failCb){
				successCb = successCb || function(){};
				failCb = failCb || function(){};
				let description = {
					type: 4,
					add_scores: orderInfo.point,
					reduce_scores: 0,
					user_id: orderInfo.user_id,
					order_id: orderInfo.order_id,
					order_form_log_id: orderInfo.order_id
				}
				let data = {
					name: 'shopping.sys.goods.pay',
					channel: 'wx_pub',
					amount: Number(orderInfo.price) * 100,
					order_no: orderInfo.type +　orderInfo.user_id + Date.parse(new Date()),
					description: JSON.stringify(description)
				}
				this.$http.post('', data).then((response) => {
					// alert(JSON.stringify(response.body.data))
					if(response.body.code === 1000) {
						var charge = response.body.data.charge;
						//提交成功
						pingpp.createPayment(charge, function(result, err) {
						    alert('result:' + JSON.stringify(result) + 'err:' + JSON.stringify(err));
						    if (result == "success") {
						    	alert('支付成功！您的流量或话费将稍后到账。');
							}
						    else {
						    	// alert(JSON.stringify(err))
						    	// failCb();
						    }
						});
					}
				})
			},
		pay(){
			wxPayLogin().then(code => {
				this.$http.post('', {
					name: 'shopping.sys.pay.getopenid',
					code: code
				}).then(r => {
					this.$http.post('',{
							name:'shopping.sys.orderSubmit',
							mobile: 13750011111,
							goods_id: 26,
							shopMobile:'0'
						}).then((response) => {
							if(response.body.code !== 1000) {
								alert(response.body.msg);
								return;
							}
							let orderInfo = response.body.data;
							orderInfo.type = 'a';
							this.pingpay(orderInfo, () => {
							}, () => {
							});
						})
				})
			})
		}
	}
}
</script>